<?php
	define('CONFIG_FILE',true);
	include '../config.php';
	
	define('DB_FILE',true);
	require_once '../Tool/DB/SqlDBManager.class.php';

	session_start();
	if (empty($_SESSION["userType"])) {
		$url="../Index.php";
		include_once '../Tool/Common/Redirect.php';
		exit();
	}

	if($_SESSION["userType"]!="System Admin"){
        $url="../Index.php";
		include_once '../Tool/Common/Redirect.php';
		exit();
    }
    
	$sqlDBManager = new SqlDBManager();
	
	$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
	$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
	$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'Date';
	$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';
	$offset = ($page-1)*$rows;
	$result = array();
	
	$sql = "select count(*) from holiday";
	$res=$sqlDBManager->queryRow($sql);
		if(!empty($res)){
			$result["total"] = $res[0];
		}
		else{
			$result["total"] = 0;
		}
		
	$sql ="select * from holiday order by $sort $order limit $offset,$rows";	
	$res=$sqlDBManager->queryRows($sql);
	$items = array();		
	if(!empty($res)){
		for($i=0;$i<count($res);$i++)
		{
			array_push($items, $res[$i]);
		}	
	}
	
	//close connection
  	$sqlDBManager->close_connect();

	$result["rows"] = $items;
	echo json_encode($result);
?>